﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【市】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto市
    /// </summary>
    public abstract class AutoCityMapping : BaseMapping<City, CityModel, CityFilterModel>
    {
        public AutoCityMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override CityModel Entity2Model(City entity)
        {
            CityModel model = new CityModel();

            model.CityID = entity.CityID;
            model.ProvinceID = entity.ProvinceID;
            model.Code = entity.Code;
            model.Name = entity.Name;
            model.Longitude = entity.Longitude;
            model.Latitude = entity.Latitude;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override City Model2Entity(CityModel model)
        {
            City entity = new City();

            entity.CityID = model.CityID;
            entity.ProvinceID = model.ProvinceID;
            entity.Code = model.Code;
            entity.Name = model.Name;
            entity.Longitude = model.Longitude;
            entity.Latitude = model.Latitude;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<City> FilterModel2Where(CityFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._CityID.HasValue)
            {
                where = where.And(x => x.CityID == filterModel._CityID.Value);
            }
            if (filterModel._CityIDs.IsNotEmpty() && filterModel._CityIDs.Count > 0)
            {
                where = where.And(x => filterModel._CityIDs.Contains(x.CityID));
            }
            if (filterModel._NotCityIDs.IsNotEmpty() && filterModel._NotCityIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotCityIDs.Contains(x.CityID));
            }

            //省
            if (filterModel._ProvinceID.HasValue)
            {
                where = where.And(x => x.ProvinceID == filterModel._ProvinceID.Value);
            }
            if (filterModel._ProvinceIDs.IsNotEmpty() && filterModel._ProvinceIDs.Count > 0)
            {
                where = where.And(x => filterModel._ProvinceIDs.Contains(x.ProvinceID));
            }
            if (filterModel._NotProvinceIDs.IsNotEmpty() && filterModel._NotProvinceIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotProvinceIDs.Contains(x.ProvinceID));
            }

            //区号
            if (!string.IsNullOrEmpty(filterModel._Code))
            {
                where = where.And(x => x.Code.Contains(filterModel._Code));
            }
            if (!string.IsNullOrEmpty(filterModel._CodeEqual))
            {
                where = where.And(x => x.Code == filterModel._CodeEqual);
            }

            //名称
            if (!string.IsNullOrEmpty(filterModel._Name))
            {
                where = where.And(x => x.Name.Contains(filterModel._Name));
            }
            if (!string.IsNullOrEmpty(filterModel._NameEqual))
            {
                where = where.And(x => x.Name == filterModel._NameEqual);
            }

            return where;
        }
        #endregion
    }
}

